package com.hmsproject.db.inmemorydb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hmsproject.datastructures.User;
import com.hmsproject.datastructures.transactions.Transaction;


public class TransactionMap {
	
	public static TransactionMap INSTANCE = new TransactionMap();
	
	private Map<String,Transaction> transactionMapper;
	
	private TransactionMap(){
		transactionMapper = new HashMap<String,Transaction>();
	}
	
	public Transaction getTransaction(String transactionId){
		return transactionMapper.get(transactionId);
	}
	
	public Transaction getTransaction(String userId,String dealId){
		
		Transaction transaction = null;
		
		Collection<Transaction> transactionList = transactionMapper.values();
		
		Iterator<Transaction> iterator = transactionList.iterator();

		while(iterator.hasNext()){
			Transaction tmptransaction = iterator.next();
			
			if(tmptransaction.getUserId().equals(userId) && tmptransaction.getDeal().getDealId().equals(dealId)){
				transaction = tmptransaction;
				break;
			}
		}		
		
		return transaction;
	}
	
	public List<Transaction> getTransactionList(String userId){
		
		List<Transaction> transactionListReturn = new ArrayList<Transaction>();
		
		Collection<Transaction> transactionList = transactionMapper.values();
		
		Iterator<Transaction> iterator = transactionList.iterator();

		while(iterator.hasNext()){
			Transaction tmptransaction = iterator.next();
			
			if(tmptransaction.getUserId().equals(userId)){
				transactionListReturn.add(tmptransaction);
			}
		}		
		
		return transactionListReturn;
	}	
	
	public void addTransaction(Transaction transaction){
		transactionMapper.put(transaction.getTransactionId(), transaction);
	}	
	
	public List<Transaction> getTransactionListForDeal(String dealId){
		List<Transaction> transactionListReturn = new ArrayList<Transaction>();
		
		Collection<Transaction> transactionList = transactionMapper.values();
		
		Iterator<Transaction> iterator = transactionList.iterator();

		while(iterator.hasNext()){
			Transaction tmptransaction = iterator.next();
			
			if(tmptransaction.getDeal().getDealId().equals(dealId)){
				transactionListReturn.add(tmptransaction);
			}
		}		
		
		return transactionListReturn;		
	}	
	
	public int getTransactionRoombookedForDeal(String dealId){
		
		int totalAlreadyBooked = 0;

		
		Collection<Transaction> transactionList = transactionMapper.values();
		
		Iterator<Transaction> iterator = transactionList.iterator();

		while(iterator.hasNext()){
			Transaction tmptransaction = iterator.next();
			
			if(tmptransaction.getDeal().getDealId().equals(dealId)){
				totalAlreadyBooked += tmptransaction.getNoOfRoomsBooked();
			}
		}		
		
		return totalAlreadyBooked;		
	}		
	
}
